import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { CategoryType } from '@/types/furniture'
import {
  getCategoryList,
  addCategory,
  updateCategory,
  deleteCategory,
  updateCategoryStatus
} from '@/api/category'

export const useCategoryStore = defineStore('category', () => {
  const categoryList = ref<CategoryType[]>([])
  const total = ref(0)
  const loading = ref(false)

  // 获取分类列表
  async function fetchCategoryList(params: any) {
    loading.value = true
    try {
      const { list, total: totalCount } = await getCategoryList(params)
      categoryList.value = list
      total.value = totalCount
    } finally {
      loading.value = false
    }
  }

  // 添加分类
  async function createCategory(data: any) {
    const id = await addCategory(data)
    return id
  }

  // 更新分类
  async function modifyCategory(data: any) {
    await updateCategory(data)
  }

  // 删除分类
  async function removeCategory(id: string) {
    await deleteCategory(id)
  }

  // 更新状态
  async function toggleStatus(id: string, status: number) {
    await updateCategoryStatus(id, status)
  }

  return {
    categoryList,
    total,
    loading,
    fetchCategoryList,
    createCategory,
    modifyCategory,
    removeCategory,
    toggleStatus
  }
}) 